[UVa] 10887 - Concatenation of Languages

題意

現有A集合和B集合,請輸出兩集合所能形成的字串數量。

注意

會有空字串、空白,以及這種情況:「aaa,baa,ab都會產生同樣的字串」。

心得

先偷懶用STLSet,做法也是有點暴力,之後再研究其它解法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <set>
using namespace std;
int main()
{
set <string> A , B , C ;
int T ;
cin >> T ;
for ( int M , N , t = 0 ; t < T && cin >> M >> N ; t ++ ) {
A.clear() , B.clear() , C.clear() ;
string input ;
cin.ignore() ;
for ( int m = 0 ; m < M && getline(cin,input) ; m ++ ){
A.insert(input) ;
}
for ( int n = 0 ; n < N && getline(cin,input) ; n ++ ){
B.insert(input) ;
}
set<string>::iterator it , it2;
for ( it = A.begin() ; it != A.end() ; it ++ ){
for ( it2 = B.begin() ; it2 != B.end() ; it2 ++ ){
C.insert(*it+*it2) ;
}
}
cout << "Case " << t+1 << ": " << C.size() << endl ;
}
return 0;
}